/**
 * @author chenzhuo
 * @time 2022-12-2 9:00 AM
 * @description 定义pinia状态数据仓库，实现状态数据共享
 */
 import { createPinia, defineStore, } from 'pinia'
 import { deCodeApi, enCodeApi } from '../utils'
 
 const Pinia = createPinia()
 
 let token = sessionStorage.getItem('now-token')
 let userRole = sessionStorage.getItem('userRole')||{}
 let userInfo = sessionStorage.getItem('userInfo')||{}
 
 const commonDefinition = defineStore(
     'common',
     {
         /** 定义要缓存的数据对象并且赋初始值 */
         state() {
             return {
                 token,
                 userInfo,
                 userRole,
             }
         },
         /** 定义getters获取数据 */
         getters: {
             getToken() {
                 return deCodeApi(this.token)
             },
             getUserInfo() {
                return  JSON.parse(deCodeApi(this.userInfo))
            },
            getUserRole() {
                return  JSON.parse(deCodeApi(this.userRole))
            }
         },
         /** 赋值方法--actions */
         actions: {
             setToken(_token) {
                 _token = enCodeApi(_token)
                 this.token = _token
                 sessionStorage.setItem('now-token', _token)
             },
             setUserInfo(_userInfo){
                _userInfo = enCodeApi(JSON.stringify(_userInfo))
                this.userInfo = _userInfo
                sessionStorage.setItem('userInfo',_userInfo)
             },
             setUserRole(_userRole){
                _userRole = enCodeApi(JSON.stringify(_userRole))
                this.userRole = _userRole
                sessionStorage.setItem('userRole',_userRole)
             }
         }
     }
 )
 
 export {
     Pinia,
     commonDefinition
 }